using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FitnessClub.Bll.StaffManager;
using System.Collections.Generic;

public partial class EmployeesServices : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int employeeID = -1;
            if (!StateManager.Instance.IsNullInSession(SessionKeys.EmployeeId))
            {
                employeeID = StateManager.Instance.GetFromSession<int>(SessionKeys.EmployeeId);
                StateManager.Instance.RemoveFromSession(SessionKeys.EmployeeId);
            }
            List<ResListEmp> tmpSource = Delegate.getAllEmployee();
            foreach (ResListEmp tmpItem in tmpSource)
            {
                ListItem tmpListItem = new ListItem();
                tmpListItem.Value = tmpItem.Id.ToString();
                tmpListItem.Text = tmpItem.Surname + " " + tmpItem.Name;
                ddlPracownik.Items.Add(tmpListItem);
                if (tmpItem.Id == employeeID)
                    ddlPracownik.SelectedIndex = ddlPracownik.Items.Count-1;
            }
            GridView1.DataSource = Delegate.getAllEmployeeServices();
            GridView1.DataBind();
        }
    }
    protected void btnPrzyporzadkuj_Click(object sender, EventArgs e)
    {
        ReqBEmp req = new ReqBEmp();
        req.ServiceID = int.Parse(ddlUsluga.SelectedValue);
        req.EmployeeID = int.Parse(ddlPracownik.SelectedValue);
        bool result = Delegate.bindEmployeeToService(req);
        if (!result)
            lblBindError.Visible = true;
        else
        {
            lblBindError.Visible = false;
            Response.Redirect("EmployeesServices.aspx");
        }
    }
    protected void UsunPowiazanie(object sender, EventArgs e)
    {
        Button but = (Button)sender;
        GridViewRow gwr = GridView1.Rows[int.Parse(but.CommandName)];
        ReqUBEmp req = new ReqUBEmp();
        req.EmployeeID = int.Parse(((HiddenField)gwr.FindControl("hfId_pracownik")).Value);
        req.ServiceID = int.Parse(((HiddenField)gwr.FindControl("hfId_usluga")).Value);
        req.Time = new DateTime(long.Parse(((HiddenField)gwr.FindControl("hftime")).Value));
        bool result = Delegate.unbindEmployeeFromService(req);
        if (!result)
            lblUnbindError.Visible = true;
        else
        {
            lblUnbindError.Visible = false;
            Response.Redirect("EmployeesServices.aspx");
        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType != DataControlRowType.DataRow)
        {
            return;
        }

        ResListEmpServices res = (ResListEmpServices)e.Row.DataItem;
        ((Label)e.Row.FindControl("lblPracownik")).Text = res.Employee.Surname + " " + res.Employee.Name;
        ((Label)e.Row.FindControl("lblUsluga")).Text = res.Service.Name;
        ((HiddenField)e.Row.FindControl("hfId_pracownik")).Value = res.Employee.Id.ToString();
        ((HiddenField)e.Row.FindControl("hfId_usluga")).Value = res.Service.Id.ToString();
        ((HiddenField)e.Row.FindControl("hftime")).Value = res.Time.ToBinary().ToString();
        Button btn = ((Button)e.Row.FindControl("btnUsun"));
    }
    protected void GridView1_DataBound(object sender, EventArgs e)
    {
        for (int k = 0; k < GridView1.Rows.Count; k++)
        {
            ((Button)GridView1.Rows[k].FindControl("btnUsun")).CommandName = k.ToString();
        }
    }
    protected void btnPowrot_Click(object sender, EventArgs e)
    {
        Response.Redirect("Employees.aspx");
    }
}
